package com.simple.datax.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.simple.datax.bo.IDataxDictionaryBO;
import com.simple.datax.bo.IDataxNodeChannelBO;
import com.simple.datax.bo.IDataxSelfNodePropBO;
import com.simple.datax.vo.DataxNodeChannelVO;
import com.simple.datax.vo.DataxSelfNodePropVO;
import com.simple.util.common.Constants;
import com.simple.util.object.JFValidationBean;
import com.simple.util.object.PageInfoBean;

@Controller
public class ChangeController extends AbstractDataxBaseController {
	private final static String JSP_PATH = "_Change";
	@Autowired
	private IDataxNodeChannelBO dataxNodeChannelBO;
	@Autowired
	private IDataxDictionaryBO dataxDictionaryBO;
	@Autowired
	private IDataxSelfNodePropBO dataxSelfNodePropBO;

	@RequestMapping("/list")
	public String init(
			@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "fcNodeSelect", required = false) String fcNodeSelect,
			@RequestParam(value = "fcSendTypeSelect", required = false) String fcSendTypeSelect,
			@RequestParam(value = "fcRecvTypeSelect", required = false) String fcRecvTypeSelect,
			@RequestParam(value = "fiFlagSelect", required = false) String fiFlagSelect,
			HttpServletRequest request, HttpServletResponse response) {

		final PageInfoBean pageInfoBean = new PageInfoBean(pageNo);
		final List<DataxNodeChannelVO> dataxNodeChannelVOList = dataxNodeChannelBO
				.findDataxNodeChannelVOByPage(pageInfoBean, fcNodeSelect,
						fcSendTypeSelect, fcRecvTypeSelect, fiFlagSelect);
		request.setAttribute("dataxNodeChannelVOList", dataxNodeChannelVOList);

		final Map<String, String> catagoryTransfersMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_TRANSFERSWAY);
		final Map<String, String> catagoryMessageFormatMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGEFORMAT);
		final Map<String, String> catagoryCharacterMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGECHARACTER);
		final Map<String, String> catagoryMessageTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGETYPE);
		request.setAttribute("catagoryTransfersMap", catagoryTransfersMap);
		request.setAttribute("catagoryMessageFormatMap",
				catagoryMessageFormatMap);
		request.setAttribute("catagoryCharacterMap", catagoryCharacterMap);
		request.setAttribute("catagoryMessageTypeMap", catagoryMessageTypeMap);
		request.setAttribute("catagoryStateMap", Constants.STATE_MAP);

		final String pageInfo = super.generatePageInfo(request, response,
				pageInfoBean, "goPage");
		request.setAttribute("pageInfo", pageInfo);
		request.setAttribute("pageNo", pageNo);
		request.setAttribute("fcNodeSelect", fcNodeSelect);
		request.setAttribute("fcSendTypeSelect", fcSendTypeSelect);
		request.setAttribute("fcRecvTypeSelect", fcRecvTypeSelect);
		request.setAttribute("fiFlagSelect", fiFlagSelect);

		return getJspPath("list");
	}

	@RequestMapping("/updateInit")
	public String updateInit(HttpServletRequest request,
			HttpServletResponse response) {

		final Map<String, String> catagoryTransfersMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_TRANSFERSWAY);
		final Map<String, String> catagoryMessageFormatMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGEFORMAT);
		final Map<String, String> catagoryCharacterMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGECHARACTER);
		final Map<String, String> catagoryMessageTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGETYPE);
		request.setAttribute("catagoryTransfersMap", catagoryTransfersMap);
		request.setAttribute("catagoryMessageFormatMap",
				catagoryMessageFormatMap);
		request.setAttribute("catagoryCharacterMap", catagoryCharacterMap);
		request.setAttribute("catagoryMessageTypeMap", catagoryMessageTypeMap);
		
		DataxSelfNodePropVO dataxSelfNodePropVO = dataxSelfNodePropBO.getSelfNodeProp();
		request.setAttribute("dataxSelfNodePropVO",
				dataxSelfNodePropVO);
		return getJspPath("update_init");
	}

	@RequestMapping("/update")
	public String update(
			@RequestParam(value = "fcNodeSelect", required = false) String fcNodeSelect,
			@RequestParam(value = "fcNodeDescSelect", required = false) String fcNodeDescSelect,
			@RequestParam(value = "fcSendTypeSelect", required = false) String fcSendTypeSelect,
			@RequestParam(value = "fcSendParamSelect", required = false) String fcSendParamSelect,
			@RequestParam(value = "fcRecvTypeSelect", required = false) String fcRecvTypeSelect,
			@RequestParam(value = "fcRecvParamSelect", required = false) String fcRecvParamSelect,
			@RequestParam(value = "fcMsgFormateId", required = false) String fcMsgFormateId,
			@RequestParam(value = "fcMsgTypeSelect", required = false) String fcMsgTypeSelect,
			@RequestParam(value = "fcMsgCharsetSelect", required = false) String fcMsgCharsetSelect,
			HttpServletRequest request, HttpServletResponse response) {
		final List<JFValidationBean> errorList = getErrorList();
		dataxSelfNodePropBO.update(fcNodeSelect, fcNodeDescSelect,
				fcSendTypeSelect, fcSendParamSelect, fcRecvTypeSelect,
				fcRecvParamSelect, fcMsgFormateId, fcMsgTypeSelect,
				fcMsgCharsetSelect);
		errorList.add(new JFValidationBean("message.system.sucess"));
		return updateInit(request, response);
	}

	@Override
	protected String getBaseJspPath() {
		return JSP_PATH;
	}

}
